<h1>Profil du joueur : <%= @player.pseudo %></h1>

<div class="title_w_bg"> Saisons du joueur </div>
<br>
<% if @player.seasons.empty? %>
  Pas de saison
<% else %>
  <table class="leaderboard">
    <tr>
      <% if !mobile_device? && user_signed_in?%>
      <th>Id season</th>
      <% else %>
      <th></th>
      <% end %>
      <th>Format</th>
      <th>Comp.</th>
      <th>Pts</th>
      <th>J.</th>
      <th>G.</th>
      <th>G.pr</th>
      <th>G.pn.</th>
      <th>N.</th>
      <th>D.</th>
      <th>D.pr</th>
      <th>D.pn.</th>
      <th>bp.</th>
      <th>bc.</th>
      <th class="lb_br">Diff.</th>
    </tr>
    <br>
  <% @player.seasons.each { |pls|  %>
    <tr>
      <% if !mobile_device? && user_signed_in? %>
      <td><%= pls.id %></td>
      <% else %>
      <td>-</td>
      <% end %>
      <td><%= pls.tournament.try(:format) %></td>
      <% if !pls.tournament.nil? %>
      <td class="lead_team"><%= link_to pls.tournament.name, tournament_path(pls.tournament) %></td>
      <% else %> <td>Nom tournoi manquant</td>
      <% end %>
      <td><b><%= pls.points %></b></td>
      <td class="lb_br"><%= pls.teams.count %> </td>
      <td><%= pls.win %> </td>
      <td><%= pls.win_prol %> </td>
      <td class="lb_br"><%= pls.win_peno %> </td>
      <td class="lb_br"><%= pls.draw %> </td>
      <td><%= pls.lose %> </td>
      <td><%= pls.lose_prol %> </td>
      <td class="lb_br"><%= pls.lose_peno %> </td>
      <td><%= @bp = Team.all.where("season_id= '#{pls.id}'").sum(:score) %></td>
      <td><%= @bc = (Team.where("season_id= '#{pls.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("season_id= '#{pls.id}'").sum(:score)) %></td>
      <td class="lb_br"><%= @bp-@bc %> </td>
    </tr>
    <tr>
      <td colspan = 5></td>
      <td colspan = 3><%= pls.win + pls.win_peno + pls.win_prol %> Victoires </td>
      <td><%= pls.draw %> Nuls </td>
      <td colspan = 3><%= pls.lose + pls.lose_prol + pls.lose_peno %> Défaites </td>
      <td colspan = 3></td>
    </tr>
  <% } %>
  </table>
<% end %>

<br>

<div class="title_w_bg">Statistiques globales</div>
<br>
<table class="leaderboard">
  <tr>
    <th> </th>
    <th> </th>
    <th>Pts</th>
    <th>J.</th>
    <th>G.</th>
    <th>G.pr</th>
    <th>G.pn.</th>
    <th>N.</th>
    <th>D.</th>
    <th>D.pr</th>
    <th>D.pn.</th>
    <th>bp.</th>
    <th>bc.</th>
    <th class="lb_br">Diff.</th>
  </tr>
  <br>

  <tr>
    <td></td>
    <td class="lead_team"><%= @player.pseudo %></td>
    <td><b><%= @player.seasons.sum(:points) %></b></td>
    <td class="lb_br"><%= @player.teams.count %> </td>
    <td><%= @player.seasons.sum(:win) %> </td>
    <td><%= @player.seasons.sum(:win_prol) %> </td>
    <td class="lb_br"><%= @player.seasons.sum(:win_peno) %> </td>
    <td class="lb_br"><%= @player.seasons.sum(:draw) %> </td>
    <td><%= @player.seasons.sum(:lose) %> </td>
    <td><%= @player.seasons.sum(:lose_prol) %> </td>
    <td class="lb_br"><%= @player.seasons.sum(:lose_peno) %> </td>
    <td><%= Team.all.where("player_id= '#{@player.id}'").sum(:score) %></td>
    <td><%= (Team.where("player_id= '#{@player.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("player_id= '#{@player.id}'").sum(:score)) %></td>
    <td class="lb_br"><b><%= (Team.all.where("player_id= '#{@player.id}'").sum(:score))-((Team.where("player_id= '#{@player.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("player_id= '#{@player.id}'").sum(:score))) %></b></td>
  </tr>

  <tr>
    <td colspan = 4></td>
    <td colspan = 3><%= @player.seasons.sum(:win) + @player.seasons.sum(:win_peno) +@player.seasons.sum(:win_prol) %> Victoires </td>
    <td><%= @player.seasons.sum(:draw)%> Nuls </td>
    <td colspan = 3><%= @player.seasons.sum(:lose) + @player.seasons.sum(:lose_prol) + @player.seasons.sum(:lose_peno) %> Défaites </td>
    <td colspan = 3></td>
  </tr>

  <tr>
    <td colspan = 2>Rencontres directes</td>

    <td colspan = 13></td>
  </tr>

  <% @players.ids.each { |pl|  %>
    <% if !(@player.id == pl) %>
        <% rencontre = Match.includes(:teams).where("teams.player_id": [pl, @player.id]).references(:teams).ids.each_with_object(Hash.new(0)) { |rencontre,counts| counts[rencontre] += 1 }.select{|k,v| v == 2}.keys %>
        <% p rencontre %>
        <% if rencontre.empty? %>
        <!--
          <td></td>
          <td class="lead_team" colspan = 3 > vs. <%= Player.find(pl).pseudo %> : </td>
          <td colspan = 6>Pas de rencontre entre ces deux joueurs.</td>
          <td colspan = 3></td>
        -->
        <% else %>
          <% mixmatches = rencontre.each_with_object(Hash.new(0)) { |r,counts| counts[@player.teams.where(match_id:r).first.status] += 1 } %>
          <% p mixmatches %>
          <tr>
            <td></td>
            <td class="lead_team" colspan = 3 > vs. <%= Player.find(pl).pseudo %> : </td>
            <td colspan = 3><%= mixmatches["winner"] %> Victoire(s) </td>
            <td><%= mixmatches["draw"] %> Nul(s) </td>
            <td colspan = 3><%= mixmatches["loser"] %> Défaite(s) </td>
            <td colspan = 3></td>
          </tr>
        <% end %>
    <% end %>
  <% } %>

  <tr>
    <td colspan = 2>Rencontres par type de matchs :</td>
    <td></td>
    <td>J.</td>
    <td colspan = 10></td>
  </tr>
  <tr>
      <td></td>
      <td class="lead_team" colspan = 2 >Mode Normal :</td>
      <td><%= (Match.joins(:players).where(["mode=? and players.id=?",'Normal', @player.id]).count) %></td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Normal', @player.id, "winner"]).count) %> Victoire(s) </td>
      <td><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Normal', @player.id, "draw"]).count) %> Nul(s)</td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Normal', @player.id, "loser"]).count) %> Défaite(s) </td>
      <td colspan = 3></td>
  </tr>
  <tr>
      <td></td>
      <td class="lead_team" colspan = 2 >Mode Survie :</td>
      <td><%= (Match.joins(:players).where(["mode=? and players.id=?",'Survie', @player.id]).count) %></td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Survie', @player.id, "winner"]).count) %> Victoire(s) </td>
      <td><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Survie', @player.id, "draw"]).count) %> Nul(s)</td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Survie', @player.id, "loser"]).count) %> Défaite(s) </td>
      <td colspan = 3></td>
  </tr>
  <tr>
      <td></td>
      <td class="lead_team" colspan = 2 >Mode Sans règle :</td>
      <td><%= (Match.joins(:players).where(["mode=? and players.id=?",'Sans règle', @player.id]).count) %></td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Sans règle', @player.id, "winner"]).count) %> Victoire(s) </td>
      <td><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Sans règle', @player.id, "draw"]).count) %> Nul(s)</td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Sans règle', @player.id, "loser"]).count) %> Défaite(s) </td>
      <td colspan = 3></td>
  </tr>
  </tr>
  <tr>
      <td></td>
      <td class="lead_team" colspan = 2 >Mode Distance :</td>
      <td><%= (Match.joins(:players).where(["mode=? and players.id=?",'Distance', @player.id]).count) %></td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Distance', @player.id, "winner"]).count) %> Victoire(s) </td>
      <td><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Distance', @player.id, "draw"]).count) %> Nul(s)</td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Distance', @player.id, "loser"]).count) %> Défaite(s) </td>
      <td colspan = 3></td>
  </tr>
  <tr>
      <td></td>
      <td class="lead_team" colspan = 2 >Mode Têtes et volées :</td>
      <td><%= (Match.joins(:players).where(["mode=? and players.id=?",'Têtes et volées', @player.id]).count) %></td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Têtes et volées', @player.id, "winner"]).count) %> Victoire(s) </td>
      <td><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Têtes et volées', @player.id, "draw"]).count) %> Nul(s)</td>
      <td colspan = 3><%= (Team.joins(:player).joins(:match).where(["matches.mode=? and players.id=? and status=?",'Têtes et volées', @player.id, "loser"]).count) %> Défaite(s) </td>
      <td colspan = 3></td>
  </tr>
  </tr>
</table>

<!--
<div class="title_w_bg">Rencontres directes </div>
<br>
<div id="country">
<%= select_tag :player,
    options_for_select(@players.collect{|p| [p.pseudo, p.id]},
    selected: params[:player]),
    prompt: "Selectionner un adversaire", class: "form-control",
    onchange: 'your_onchange_handler()' %>
</div>
-->

<div class="title_w_bg">Equipes les plus utilisées </div>
<br>
<div class="mobile-content_une">
<% club = @player.teams.each_with_object(Hash.new(0)) do |tm,counts| counts[tm.club.name]+= 1 end %>
<% sortcountclub = club.sort_by{|k,v| v}.reverse%>
<% sortcountclub.take(5).each{ |a,b| %>
  <%= a %>, <%= b %> fois.
  <br>
<% } %>
</div>
<br>
<div class="title_w_bg">Records et chiffres </div>
<div class="mobile-content_une">
- Record de buts marqués en un match : <%= maxgoal = @player.teams.maximum("score") %>
</div>
<% if @player.teams.empty? %>
  "Pas de match pour l'instant"
<% else %>
<table class="scoreboard">
  <tr>
    <% if !mobile_device? %>
    <th>Id du match</th>
    <% end %>
    <th>Compétition</th>
    <th>Mode</th>
    <th>Joueur 1</th>
    <th>Team 1</th>
    <th>Score</th>
    <th>Team 2</th>
    <th>Joueur 2</th>
  </tr>
  <br>
  <% match_maxgoal = @player.teams.where(score: maxgoal).first.match %>
  <tr>
    <% if !mobile_device? %>
    <td><%= match_maxgoal.id %></td>
    <% end %>
    <td><%= match_maxgoal.tournament.name %></td>
    <td><%= match_maxgoal.mode %></td>
    <td><%= link_to Player.find(match_maxgoal.teams.first.player_id).pseudo, player_path(match_maxgoal.teams.first.player_id) %> </td>
    <% if match_maxgoal.teams.first.status == "winner" %>
      <td class="statuswinner"><%= match_maxgoal.teams.first.club.name %></td>
      <% else %>
      <td><%= match_maxgoal.teams.first.club.name %></td>
    <% end %>
    <td><div class="tab_score">
          <%= match_maxgoal.teams.first.score %> - <%= match_maxgoal.teams.last.score %>
        </div>
        <span class="tab_score_prol">
        <%= (match_maxgoal.prolongations && !match_maxgoal.teams.first.prol_score) ? "a.p" : "" %>
        <%= match_maxgoal.teams.first.prol_score %><%= match_maxgoal.teams.first.prol_score == nil ? '' : ' tab ' %><%= match_maxgoal.teams.last.prol_score %>
        </span>
    </td>

    <% if match_maxgoal.teams.last.status == "winner" %>
      <td class="statuswinner"><%= match_maxgoal.teams.last.club.name %></td>
      <% else %>
      <td><%= match_maxgoal.teams.last.club.name %></td>
    <% end %>
    <td><%= link_to Player.find(match_maxgoal.teams.last.player_id).pseudo, player_path(match_maxgoal.teams.last.player_id) %> </td>

  </tr>
</table>
<% end %>

<br>
<div class="title_w_bg">Tous les matchs du joueur</div>
<table class="scoreboard">
  <tr>
    <% if !mobile_device? %>
    <th>ID</th>
    <% end %>
    <th>Compétition</th>
    <th>Mode</th>
    <th>Joueur 1</th>
    <th>Team 1</th>
    <th>Score</th>
    <th>Team 2</th>
    <th>Joueur 2</th>
    <!--
    <th>Résultat</th>
    -->
  </tr>
  <br>
  <% Match.includes(:players).where("players.id = ?", @player.id).references(:players).sort_by{|m| -m[:id]}.each { |match| %>
    <% if match.teams.empty? %>
    <tr>
      <td><%= match.id %></td>
      <td>Pas de match enegistré...</td>
    </tr>
    <% else %>
    <tr>
      <% if !mobile_device? %>
      <td><%= match.id %></td>
      <% end %>
      <td><%= match.tournament.name %></td>
      <td><%= match.mode %></td>
      <td><%= link_to Player.find(match.teams.first.player_id).pseudo, player_path(match.teams.first.player_id) %> </td>
      <% if match.teams.first.status == "winner" %>
        <td class="statuswinner"><%= match.teams.first.club.name %></td>
        <% else %>
        <td><%= match.teams.first.club.name %></td>
      <% end %>
      <td><div class="tab_score">
            <%= match.teams.first.score %> - <%= match.teams.last.score %>
          </div>
          <span class="tab_score_prol">
          <%= (match.prolongations && !match.teams.first.prol_score) ? "a.p" : "" %>
          <%= match.teams.first.prol_score %><%= match.teams.first.prol_score == nil ? '' : ' tab ' %><%= match.teams.last.prol_score %>
          </span>
      </td>

      <% if match.teams.last.status == "winner" %>
        <td class="statuswinner"><%= match.teams.last.club.name %></td>
        <% else %>
        <td><%= match.teams.last.club.name %></td>
      <% end %>
      <td><%= link_to Player.find(match.teams.last.player_id).pseudo, player_path(match.teams.last.player_id) %> </td>

    </tr>
  <% end %>
  <% } %>

</table>

<br><br>
<%= link_to "Accueil", root_path %>
